package com.test.practice.collection.set;

import com.test.practice.collection.Student;

import java.util.*;

/**
 * SetDemo
 *
 * @Description: SetDemo
 * =======================
 * @Author: Jovi
 * =======================
 * @Date: Created in 2023-02-08
 * =======================
 * @Version: 1.1.2.0
 */
public class HashSetDemo {

    public static void main(String[] args) {
//        m1();
//        m2();
//        m3();

        m4();

    }

    /**
     * 定制排序
     */
    private static void m4() {
        Set set = new TreeSet<>(Comparator.comparing(Student::getName));
        set.add(new Student("aa","man"));
        set.add(new Student("dd","man"));
        set.add(new Student("cc","man"));
        set.add(new Student("ee","man"));
        set.add(new Student("ee","woman"));

        for (Object o : set) {
            System.out.println(o);
        }
    }

    /**
     * 自然排序
     */
    private static void m3() {
        //        //TreeSet 不能添加类型不同的数据对象
//        Set set = new TreeSet();
//        set.add(1122);
//        set.add(123);
//        set.add("aaa");
//        set.add("bbb");
//        set.add(123.2);
//        set.add(123);
//        System.out.println(set.size());

        Set set = new TreeSet();
        //场景1：
//        set.add(12);
//        set.add(233);
//        set.add(1442);
//        set.add(5);
//        set.add(112);


        //场景2: .ClassCastException Student cannot be cast to java.lang.Comparable
        //需要进行排序处理
        set.add(new Student("aa","man"));
        set.add(new Student("dd","man"));
        set.add(new Student("cc","man"));
        set.add(new Student("ee","man"));
        set.add(new Student("ee","woman"));

        for (Object o : set) {
            System.out.println(o);
        }
    }

    /**
     * 按照插入顺序遍历
     */
    private static void m2() {
        Set set = new LinkedHashSet();
        set.add(1122);
        set.add(123);
        set.add("aaa");
        set.add("bbb");
        set.add(123.2);
        set.add(123);
        System.out.println(set.size());

        for (Object o : set) {
            System.out.println(o);
        }
    }

    /**
     * 虽然是无序，但是不是随机，每次打印都是一样的
     */
    private static void m1() {
        Set set = new HashSet();
        set.add(1122);
        set.add(123);
        set.add("aaa");
        set.add("bbb");
        set.add(123.2);
        set.add(123);
        System.out.println(set.size());

        for (Object o : set) {
            System.out.println(o);
        }
    }
}
